﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zyy;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.WJL;
using B.S.Sterilizationcenter.Read.API.Dto;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.WJL
{
    public class GetReturnApplicationInfoCommandHanders : IRequestHandler<GetReturnApplicationInfoCommands, ApiResult<List<ReturnApplicationDTO>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public GetReturnApplicationInfoCommandHanders(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }


        /// <summary>
        /// 移动端回收记录页面信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<ReturnApplicationDTO>>> Handle(GetReturnApplicationInfoCommands request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<List<ReturnApplicationDTO>> result = new ApiResult<List<ReturnApplicationDTO>>();

                result.Code = ApiEnums.查询成功;
                result.Msg = "查询成功";
                var list = db.Queryable<ReturnApplication, Recycleitems, ItemTable>((o, i, c) => new JoinQueryInfos(JoinType.Inner, o.ReturnApplicationId == i.ReturnApplicationId, JoinType.Inner, i.ItemId == c.ItemId)).Where(o => o.IsDelete == false && o.ReturnApplicationId == request.ReturnApplicationId).Select((o, i, c) => new ReturnApplicationDTO
                {
                    ReturnApplicationId = o.ReturnApplicationId,
                    Itembarcode = o.Itembarcode,
                    ReturnNumber = o.ReturnNumber,
                    Receivingunit = o.Receivingunit,
                    ReturnDate = o.ReturnDate,
                    Applicantname = o.Applicantname,
                    urgent = o.urgent,
                    ItemId = i.ItemId,
                    Itemname = c.Itemname,
                    Itemnum = c.Itemnum,
                }).ToList();
                result.Data = mapper.Map<List<ReturnApplicationDTO>>(list.ToList());
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
